package com.fy56.platform.goodsTracking.commons.security;

import org.apache.commons.lang.StringUtils;

import java.util.Arrays;


public class StringUtil {

    /**
     * 判断字符串是否是空串
     * @param str
     * @return
     */	
	   public static boolean isString(String str){
		      if(null == str || str.length() == 0){
		    	  return true;
		      }
		      return false;
	   }
	
    public static String getFormatIp(String remoteAdress) {
        String ip = null;

        if (remoteAdress != null) {
            ip = formatRemoteIP(remoteAdress.substring(1,
                        remoteAdress.lastIndexOf(":")));
        }

        return ip;
    }

    /**
     * 把IP地址弄成定长格式，便于从SERVER返回消息好处理
     * 127.0.0.1----->127.000.000.001
     * @param remoteip
     * @return
     */
    public static String formatRemoteIP(String remoteip) {
        String result = "";

        if (remoteip == null) {
            return null;
        }

        String[] temp = remoteip.split("[.]");

        if (temp[0].length() == 1) {
            temp[0] = "00" + temp[0];
        }

        if (temp[0].length() == 2) {
            temp[0] = "0" + temp[0];
        }

        if (temp[1].length() == 1) {
            temp[1] = "00" + temp[1];
        }

        if (temp[1].length() == 2) {
            temp[1] = "0" + temp[1];
        }

        if (temp[2].length() == 1) {
            temp[2] = "00" + temp[2];
        }

        if (temp[2].length() == 2) {
            temp[2] = "0" + temp[2];
        }

        if (temp[3].length() == 1) {
            temp[3] = "00" + temp[3];
        }

        if (temp[3].length() == 2) {
            temp[3] = "0" + temp[3];
        }

        result = temp[0] + "." + temp[1] + "." + temp[2] + "." + temp[3];

        return result;
    }

    /**
     * 定位字串在原串中的指定序号处的位置， 原串中没有指定序号那么多个子串，则返回-1。
     *
     * @param str 原串
     * @param subStr 子串
     * @param no 序号
     * @return
     */
    public static int indexPosition(String str, String subStr, int no) {
        int len = subStr.length();

        int index = (-1) * len;

        for (int i = 0; i < no; i++) {
            index = str.indexOf(subStr, index + len);

            if (index == -1) {
                break;
            }
        }

        return index;
    }

    /**
     * 子串出现的次数
     *
     * @param str
     * @param subStr
     * @return
     */
    public static int appearTimes(String str, String subStr) {
        int len = subStr.length();

        int times = 0;

        int index = (-1) * len;

        while (true) {
            index = str.indexOf(subStr, index + len);

            if (index == -1) {
                break;
            } else {
                times++;
            }
        }

        return times;
    }

    /**
     * 将字串按用指定字符左或右填补到指定长度。
     *
     * @param srcValue
     * @param length
     * @param fillChar
     * @param isLeftFill
     * @return
     */
    public static String fill(String srcValue, int length, char fillChar, boolean isLeftFill) {
        if (srcValue == null) {
            srcValue = "";
        }

        int len = length - srcValue.length();

        if (len <= 0) {
            return srcValue.substring(0, length);
        }

        char[] fillChs = new char[len];
        Arrays.fill(fillChs, fillChar);

        if (isLeftFill) {
            srcValue = String.valueOf(fillChs) + srcValue;
        } else {
            srcValue += String.valueOf(fillChs);
        }

        return srcValue;
    }

    /**
     * get hex string,not separate by space
     * @param bs
     * @return
     */
    public static String valueOfHex(byte[] bs) {
        return valueOfHex(bs, false);
    }

    /**
     * get hex string
     * @param bs
     * @param spaceSeparate
     *        true--separate by space
     *        false--not separate by space
     * @return
     */
    public static String valueOfHex(byte[] bs, boolean spaceSeparate) {
        if (bs == null) {
            return "";
        }

        StringBuffer contents = new StringBuffer();

        for (int i = 0; i < bs.length; i++) {
            String hex = valueOfHex(bs[i]);

            if ((i > 0) && spaceSeparate) {
                contents.append(" ");
            }

            contents.append(hex.toUpperCase());
        }

        return contents.toString();
    }

    /**
     * get the byte's hex String
     * @param b
     * @return
     */
    public static String valueOfHex(byte b) {
        String hex = Integer.toHexString(b & 0xFF);

        if (hex.length() == 1) {
            hex = '0' + hex;
        }

        return hex;
    }

    /**
     * 把用空格隔开的十六进制字串，转换成二进制数组
     * @param hexSpaceStr
     * @return
     */
    public static byte[] valueOfHexString(String hexSpaceStr) {
        if (StringUtils.isBlank(hexSpaceStr)) {
            return null;
        }

        String[] hexs = hexSpaceStr.split(" ");
        byte[] bytes = new byte[hexs.length];

        for (int i = 0; i < hexs.length; i++) {
            String hexValue = hexs[i];

            try {
                bytes[i] = Byte.parseByte(hexValue, 16);
            } catch (NumberFormatException e) {
                int iValue = Integer.parseInt(hexValue, 16);
                bytes[i] = (byte) iValue;
            }
        }

        return bytes;
    }

    /**
     * 截取子串内容
     * @param content
     * @param startflag
     * @param endflag
     * @param no
     * @return
     */
    public static String getSubContent(String content, String startflag,String endflag, int no) {
        String nodeContent = "";

        //判断是否为null或""
        if (content.length() == 0) {
            return "";
        }

        if (content.contains(startflag) && content.contains(endflag)) {
            int beginIndex = startflag.length();
            int endIndex = indexPosition(content, endflag, no);
            nodeContent = content.substring(beginIndex, endIndex);
        }

        return nodeContent;
    }
    
    public static void main(String[] args) {
    	 byte[] str = Utility.int2Byte(Integer.parseInt("33554462"));
		 
		 int repeattype = str[0];
		 
		 int repeatunit =str[1];
		 
		 byte[] timestr = new byte[4];
		 timestr[2] = str[2];
		 timestr[3] = str[3];
		 
		 int timeout = Utility.byte2Int(timestr);
		 
    	 System.out.println(repeattype + "    " + repeatunit +"     "+ timeout);
	}
}
